package com.cy.springcloudalibaba.controllers;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cy.springcloudalibaba.fallbacks.PrintFallback;
import com.cy.springcloudalibaba.services.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @Description: 消费者
 * @ClassName: ConsumerController
 * @Author: chenyu
 * @Date: 2025/1/10 10:55
 */
@RestController
public class ConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient; // 负载均衡客户端

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FeignService feignService; // feign远程调用

    @Autowired
    private DiscoveryClient discoveryClient; // 服务发现client

    @GetMapping("/echo/{app}")
    public String echoApp(@PathVariable("app") String app) {
        // 使用discoveryClient和restTemplate结合的方式访问
        ServiceInstance serviceInstance = loadBalancerClient.choose("01-nacos-discovery-provider");
        String url = String.format("http://%s:%s/echo/%s", serviceInstance.getHost(), serviceInstance.getPort(), app);
        System.out.println("request url:" + url);

        return restTemplate.getForObject(url, String.class);
    }

    /**
     *  feign远程调用
    */
    @GetMapping("/feign/{app}")
    public String feignApp(@PathVariable("app") String app) {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("feign调用远程服务");

        return feignService.feign(app);
    }

    /**
     *  feign远程调用
     */
    @GetMapping("/feign/divide")
    public String feignDivide(@RequestParam("a") Integer a, @RequestParam("b") Integer b) {
        System.out.println("feign调用远程服务");

        return feignService.divide(a, b);
    }

    /**
     *  调用feign默认方法
     */
    @GetMapping("/feign/divide2")
    public String feignDivide2(@RequestParam("a") Integer a) {
        System.out.println("feign调用远程服务");

        return feignService.divide(a);
    }

    /**
     *  测试sentinel热点规则
     */
    @GetMapping("/feign/print")
    @SentinelResource(value="print", fallback = "fallback2", fallbackClass = PrintFallback.class) //sentinel热点规则 也可以是其他规则
    public String feignPrint(@RequestParam String a) {
        System.out.println("feign调用远程服务");
        return feignService.print(a);
    }

    /**
     *  discovery client
     */
    @GetMapping("/discovery/services/{service}")
    public Object discoveryServices(@PathVariable String service) {
        System.out.println("discovery client调用远程服务");

        return discoveryClient.getInstances(service);
    }
}
